﻿using Application.Common.Utils;
using Domain.Entities;
using Infrastructure.Persistence.Repository;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Application.Business.TestSimsilary
{
    public class SynchronousDataAction
    {
        private SummaryRepository _summaryRepository;
        public SynchronousDataAction(SummaryRepository summaryRepository)
        {
            _summaryRepository = summaryRepository;
        }

        public async void start()
        {
            try
            {
                JieBa jb = new();
                Shingling sl = new();
                Stopwatch sw = new();
                var summaries = await _summaryRepository.GetOnLineSummariesAsync(1,1000);
                List<SummarySimilary> ssRes = new();
                List<Simhash> shRes = new();
                sw.Start();
                foreach (var cur in summaries)
                {
                    var curDic = jb.Cut(sl.scrub(cur.Content ?? ""));
                    var curSh = new Simhash(cur.Id);
                    curSh.GenerateSimhash(curDic);
                    shRes.Add(curSh);
                    ssRes.Add(new() { sid = cur.Id, value = curSh.value.ToString() });
                    Console.WriteLine($"sum count:{summaries.Count()},current :{ssRes.Count()}");
                }
                sw.Stop();
                Console.WriteLine("计算hash 耗时 ：" + sw.Elapsed.TotalMilliseconds + "ms");
                await _summaryRepository.InsertSummarySimsilaries(ssRes);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }
}
